package com.cailiangchen.windows;

import com.cailiangchen.JDBC.JDBCUtil;
import com.cailiangchen.domain.User;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

public class login extends JFrame {
	User user = new User();

	//链接数据库
	private static JDBCUtil  jdbcUtil;
	static {
		try {
			jdbcUtil = new JDBCUtil();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {
		setTitle("login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(164, 49, 193, 24);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(164, 92, 193, 24);
		contentPane.add(textField_1);
		
		JLabel lblUser = new JLabel("User:");
		lblUser.setBounds(78, 52, 72, 18);
		contentPane.add(lblUser);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(78, 95, 72, 18);
		contentPane.add(lblPassword);
		
		JButton btnLogin = new JButton("login");
		btnLogin.setBounds(78, 180, 104, 27);
		contentPane.add(btnLogin);
		
		JButton btnRegister = new JButton("register");
		btnRegister.setBounds(269, 180, 104, 27);
		contentPane.add(btnRegister);

		//登陆
		btnLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				user.setUser(textField.getText());
				user.setPwd(textField_1.getText());

				if(user.getUser().equals("")||user.getPwd().equals("")){
					JOptionPane.showMessageDialog(null, "用户名和密码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}

				try {
					if(jdbcUtil.check(user)){
						chat chatFrame = new chat(user.getUser());
						chatFrame.setVisible(true);
						jdbcUtil.close();
						dispose();
						return;
					}else {
						JOptionPane.showMessageDialog(null, "登陆失败！", "错误", JOptionPane.ERROR_MESSAGE);
					}
				} catch (SQLException | IOException ex) {
					ex.printStackTrace();
				}
			}
		});

		//注册
		btnRegister.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				user.setUser(textField.getText());
				user.setPwd(textField_1.getText());

				if(user.getUser().equals("")||user.getPwd().equals("")){
					JOptionPane.showMessageDialog(null, "用户名和密码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				try {
					jdbcUtil.insertMessage(user);
				} catch (SQLException ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(null, "注册失败！", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
				JOptionPane.showMessageDialog(null, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

			}
		});
	}
}
